package com.sc.nft.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Assert;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.sc.nft.dao.EcologyPointsDao;
import com.sc.nft.entity.EcologyPoints;
import com.sc.nft.entity.dto.AdminEcologyPointsDTO;
import com.sc.nft.entity.vo.ecology.AdminEcologyPointsDetailsVO;
import com.sc.nft.entity.vo.ecology.AdminEcologyPointsPageVO;
import com.sc.nft.exception.GlobalRunTimeException;
import com.sc.nft.dao.EcologyPointsDao;
import com.sc.nft.entity.EcologyPoints;
import com.sc.nft.service.EcologyPointsService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;

/**
 * <p>
 * 生态积分表 服务实现类
 * </p>
 *
 * @author liGen
 * @since 2024-11-26
 */
@Slf4j
@RequiredArgsConstructor
@Service
public class EcologyPointsServiceImpl implements EcologyPointsService {


    private final EcologyPointsDao ecologyPointsDao;


    /**
     * 编辑生态积分
     * @param adminEcologyPointsDTO
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void editCrystal(AdminEcologyPointsDTO adminEcologyPointsDTO) {

        //新增
        if (Objects.isNull(adminEcologyPointsDTO.getId())) {
            Boolean ecologyPointsByName = ecologyPointsDao.getEcologyPointsByName(adminEcologyPointsDTO.getPointsName(), adminEcologyPointsDTO.getDaoId());
            if (ecologyPointsByName) {
                throw new GlobalRunTimeException("此品牌下积分名称已存在");
            }
            EcologyPoints ecologyPoints = new EcologyPoints();
            BeanUtil.copyProperties(adminEcologyPointsDTO, ecologyPoints);
            ecologyPoints.insert();
        } else {
            //编辑
            Boolean ecologyPointsByNameNotId = ecologyPointsDao.getEcologyPointsByNameNotId(adminEcologyPointsDTO.getPointsName(), adminEcologyPointsDTO.getId());
            if (ecologyPointsByNameNotId) {
                throw new GlobalRunTimeException("此品牌下积分名称已存在");
            }
            EcologyPoints ecologyPoints = ecologyPointsDao.getOne(adminEcologyPointsDTO.getId(), adminEcologyPointsDTO.getDaoId());
            Assert.notNull(ecologyPoints, () -> new GlobalRunTimeException("该积分配置不存在"));
            Assert.isTrue(ecologyPointsDao.updateEcologyPoints(adminEcologyPointsDTO.getId(), adminEcologyPointsDTO.getPointsName(),adminEcologyPointsDTO.getPointsImage(),
                    adminEcologyPointsDTO.getSort()), () -> new GlobalRunTimeException("更新积分失败"));
        }
    }


    /**
     * 积分列表 根据daoId
     * @param daoId
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public IPage<AdminEcologyPointsPageVO> adminEcologyPointsPage(Long daoId, Integer pageNo, Integer pageSize) {
        return ecologyPointsDao.adminEcologyPointsPage(daoId, pageNo, pageSize);
    }


    /**
     * 积分详情
     *
     * @param id
     * @return
     */
    @Override
    public AdminEcologyPointsDetailsVO adminEcologyPointsDetails(Long id) {
        return ecologyPointsDao.adminEcologyPointsDetails(id);
    }




    @Override
    public EcologyPoints getById(Long id) {
        return ecologyPointsDao.getById(id);
    }
}
